<?php

namespace App\Http\Controllers\admin;

use App\Admin\Tw;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

use think\facade\Cache;
use Validator;
class Two extends Controller
{
    //跳入页面
    public function add()
    {
       return view('two.add');
    }
    //增加功能
    public function create(Request $request)
    {
           //1.接值
        $arr=\request()->post();
        unset($arr['_token']);
        //2.验证（错误信息提示在表单中有）
        $validator = Validator::make($request->all(), [
            'title' => 'required|unique:posts|max:255',
        ]);

        if ($validator->fails()) {
            return $validator->errors()->first();
        }
        $path = $request->file('logo')->store('');
        $arr['logo']="/uploads/".date('Ymd').'/'.$path;

//        $res=DB::table('two')->insert($arr);    //DB
        $res=\App\Admin\Tw::add($arr);           //模型层
        if ($res){
//           header('refresh:1,url=show');
//            return redirect('');
            return '注册成功';
        }else{
            return '注册失败';
        }
    }
    //展示,搜索
    public function show(Request $request){
        //第一种方法:DB
//        $keyword=$request['keyword'];
//        $str = ['title' => $keyword];//为了解决搜索后得分页加得str
//       $arr=DB::table('two')->where('title','like',"%$keyword%")->paginate(3);
//        return view('two.show',['arr'=>$arr,'str'=>$str]);
        // 第二种方法:model
        $keyword=$request['keyword'];
        if ($keyword){
            $str = ['title' => $keyword];
            $arr=Tw::search($keyword);
            return view('two.show',['arr'=>$arr,'str'=>$str]);
        }else{
            $str = ['title' => $keyword];
            $arr=Tw::list();
            return view('two.show',['arr'=>$arr,'str'=>$str]);
        }

    }
    //详情
    public function infoo(){
        $id=\request()->get('id');
//        $data=DB::table('two')->where('id',$id)->find($id);
        $data=\App\Admin\Tw::edit($id);
        return view('two.infoo',['data'=>$data]);
//            header('refresh:3,url=update');
//            return '成功';
    }
    //修改
    public function update(Request $request){
        $id=$request['id'];
//        $arr=\request()->post();
//        unset($arr['_token']);
        $arr['title']=$request['title'];
        $arr['desc']=$request['desc'];
        //3.laravel框架  文件上传
        $path = $request->file('logo')->store('public');
        $arr['logo']="/uploads/".date('Ymd').'/'.$path;
//       $res=DB::table('two')->where('id',$id)->update($arr);

        //print_r($arr);exit();
        $res = Tw::upda($id,$arr);
       if ($res){
           header('refresh:3,url=show');
           return '修改成功';
       }else{
           header('refresh:3,url=update');
           return '修改失败';
       }
    }
    //删除
    public function delete(){
        $id=\request()->get('id');
        $res=DB::table('two')->where('id',$id)->delete();
        if ($res){
            header('refresh:3,url=show');
            return '删除成功';
        }else{
            header('refresh:3,url=show');
            return '删除失败';
        }
    }
    
}
